//
//  BK3DTouchFuction.m
//  bestkeep
//
//  Created by windCould on 17/1/11.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BK3DTouchFuction.h"
#import "AppDelegate.h"
#import "Mediator+BKMy.h"
#import "Mediator+BKScan.h"
#import "Mediator+BKSC.h"
#import "Mediator+BKMyOrderDetail.h"
@interface BK3DTouchFuction ()

@property (nonatomic,strong)UINavigationController * shortCutItemNav;
@property (nonatomic,strong)UIApplicationShortcutItem * shortCutItem;
//@property (nonatomic,assign)BOOL  isLogin;

@end


@implementation BK3DTouchFuction

+(void)creat3DTouchFunction
{
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0 ) {
        
        return;
    }
    
    UIApplicationShortcutIcon *iconScanCard = [UIApplicationShortcutIcon iconWithTemplateImageName:@"3dshourtcut_scanCard"];
    UIApplicationShortcutItem *shortcutItemScanCard = [[UIApplicationShortcutItem alloc] initWithType:@"scanCard" localizedTitle:@"扫一扫" localizedSubtitle:nil icon:iconScanCard userInfo:nil];
    UIApplicationShortcutIcon *iconBuyCar = [UIApplicationShortcutIcon iconWithTemplateImageName:@"3dshourtcut_gouwuche"];
    UIApplicationShortcutItem *shortcutItemBuyCar = [[UIApplicationShortcutItem alloc] initWithType:@"buyCar" localizedTitle:@"购物车" localizedSubtitle:nil icon:iconBuyCar userInfo:nil];
    UIApplicationShortcutIcon *iconVoucher = [UIApplicationShortcutIcon iconWithTemplateImageName:@"3dshourtcut_pay"];
    UIApplicationShortcutItem *shortcutItemVoucher = [[UIApplicationShortcutItem alloc] initWithType:@"payCode" localizedTitle:@"付款码" localizedSubtitle:nil icon:iconVoucher userInfo:nil];
    UIApplicationShortcutIcon *iconConfirmOrder = [UIApplicationShortcutIcon iconWithTemplateImageName:@"3dshourtcut_dingdan"];
    UIApplicationShortcutItem *itemConfirmOrder = [[UIApplicationShortcutItem alloc] initWithType:@"confirmOrder" localizedTitle:@"我的订单" localizedSubtitle:nil icon:iconConfirmOrder userInfo:nil];
    
    [UIApplication sharedApplication].shortcutItems = @[shortcutItemVoucher, shortcutItemScanCard,shortcutItemBuyCar, itemConfirmOrder];
}


-(void)acessOf3DTouchWithApplication:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem window:(UIWindow *)window
{
    if (shortcutItem) {  //从快捷键进入系统，
        
        if (window.rootViewController.childViewControllers.count < 3) {
            
            return;
        }
        
        [self touchPushAction:shortcutItem window:window]; //处理跳转事件；
    }
}

- (void)touchPushAction:(UIApplicationShortcutItem *)shortcutItem window:(UIWindow *)window
{
    
    self.shortCutItemNav = [self getCurrentViewControllerWithWindow:window];
    self.shortCutItem = shortcutItem;
    //界面加载完成时，进行跳转；
    [self performSelector:@selector(pushVC) withObject:nil afterDelay:0.3f];
}

//获取当前控制器

- (UINavigationController *) getCurrentViewControllerWithWindow:(UIWindow *)window
{
    UINavigationController *navigationController;
    
    UITabBarController *tabBarController = (UITabBarController *)window.rootViewController;
    
    if (tabBarController.selectedIndex == 0) {
        
        navigationController = window.rootViewController.childViewControllers.firstObject;
        
    }else if (tabBarController.selectedIndex == 1) {
        
        navigationController = window.rootViewController.childViewControllers[1];
        
    }else if (tabBarController.selectedIndex == 2) {
        
        navigationController = window.rootViewController.childViewControllers[2];
        
    }else if (tabBarController.selectedIndex == 3) {
        
        navigationController = window.rootViewController.childViewControllers[3];
        
    }else {
        
        navigationController = window.rootViewController.childViewControllers.firstObject;
    }
    
    return navigationController;
    
}

- (void)pushVC {
    
    if ([_shortCutItem.type isEqualToString:@"scanCard"]) {
        
        //扫一扫 快捷键进入
        UIViewController  * scanCardVC = [[Mediator sharedInstance] BKScanViewController];
        [_shortCutItemNav pushViewController:scanCardVC animated:YES];
    }
    
    if ([_shortCutItem.type isEqualToString:@"buyCar"]) {
        
        UIViewController  *buyCarVC  = [[Mediator  sharedInstance] BKSC_shoppingCarViewController];        [_shortCutItemNav    pushViewController:buyCarVC animated:YES];
    }
    
    if ([_shortCutItem.type isEqualToString:@"ownSave"]) {
        
        if (![self isLogin]) {
            
            [[Mediator sharedInstance] userGoToLoginFromOrignalController:self.shortCutItemNav.visibleViewController finishBlock:^{
            
            }];
            
        }else {
            
       
        }
    }
    
    if ([_shortCutItem.type isEqualToString:@"payCode"]) {
        
        if (![self isLogin]) {
            
            [[Mediator sharedInstance] userGoToLoginFromOrignalController:self.shortCutItemNav.visibleViewController finishBlock:^{
                
            }];
        }else {
            
            UIViewController * voucher = [[Mediator sharedInstance] BKStorePay_VC];
            [_shortCutItemNav pushViewController:voucher animated:YES];
        }
    }
    
    if ([_shortCutItem.type isEqualToString:@"confirmOrder"]) {
        
        if (![self isLogin]) {
            
            [[Mediator sharedInstance] userGoToLoginFromOrignalController:self.shortCutItemNav.visibleViewController finishBlock:^{
                
            }];
           
        }else {
            
            UIViewController * allOrdersVC = [[Mediator sharedInstance] GetBKMyOrderViewController];
            [_shortCutItemNav pushViewController:allOrdersVC animated:YES];
        }
    }
}

-(BOOL)isLogin {
    Mediator *mediator = [Mediator sharedInstance];
    NSDictionary *dic = [mediator BKMyUserInfo];
    NSNumber *result = dic[BKMyUserIsLoginKey];
    if ([result intValue] == 0) {
    
        return NO;
    }
    
    return YES;
}

@end
